import React, { Component } from 'react'
import styles from './index.module.scss'
import { Card, Button, Checkbox, Form, Input, message } from 'antd'
import logo from '../../assets/imags/logo.png'
import { login } from '../../api/user'
import '../../utills/storage'
import { setToken } from '../../utills/storage'

export default class Login extends Component {
  state = {
    loading: false,
  }
  render() {
    return (
      <div className={styles.login}>
        <Card className="login-container">
          <img src={logo} alt="" className="login-logo" />
          <Form
            size="large"
            onFinish={this.onFinish}
            initialValues={{
              mobile: '13911111111',
              code: '246810',
              agree: true,
            }}
          >
            <Form.Item
              name="mobile"
              validateTrigger={['onChange', 'onBlur']}
              rules={[
                {
                  required: true,
                  message: '手机号不能为空',
                  // validateTrigger: 'onBlur',
                },
                {
                  pattern: /^1[3-9]\d{9}$/,
                  message: '手机号格式错误',
                  validateTrigger: 'onBlur',
                },
              ]}
            >
              <Input placeholder="请输入手机号" autoComplete="off" />
            </Form.Item>

            <Form.Item
              name="code"
              validateTrigger={['onChange', 'onBlur']}
              /*  */
              rules={[
                {
                  required: true,
                  message: '验证码不能为空',
                  // validateTrigger: 'onBlur',
                },
                {
                  pattern: /^\d{6}$/,
                  message: '验证码格式错误',
                  validateTrigger: 'onBlur',
                },
              ]}
            >
              <Input placeholder="请输入验证码" autoComplete="off" />
            </Form.Item>

            <Form.Item
              valuePropName="checked"
              name="agree"
              rules={[
                {
                  validator(rules, value) {
                    if (value) {
                      return Promise.resolve()
                    } else {
                      return Promise.reject(
                        new Error('请阅读并同意隐私条款和用户协议')
                      )
                    }
                  },
                },
              ]}
            >
              <Checkbox>
                我已阅读并同意<a href="">隐私条款</a>和<a href="">用户协议</a>
              </Checkbox>
            </Form.Item>

            <Form.Item>
              <Button
                type="primary"
                htmlType="submit"
                block
                loading={this.state.loading}
              >
                登录
              </Button>
            </Form.Item>
          </Form>
        </Card>
      </div>
    )
  }

  onFinish = async ({ mobile, code }) => {
    // console.log('this.props', this.props)
    this.setState({
      loading: true,
    })
    try {
      const res = await login(mobile, code)
      // console.log(res)
      setToken(res.data.token)
      message.success('登录成功', 0.8, () => {
        if (this.props.location.state) {
          this.props.history.push(this.props.location.state.from)
        } else {
          this.props.history.push('/home')
        }
      })
    } catch (error) {
      console.log(error)
      message.warning(error.response.data.message, 0.8, () => {
        this.setState({
          loading: false,
        })
      })
    }
  }
}
